<template>
  <div id="insertDiv">
    <div>
      <Form ref="lightAdd" :model="formModal" :rules="formMsgRule" :label-width="120">
        <FormItem label="登录账号：" prop="userName">
          <Input v-model="formModal.userName"/>
        </FormItem>
        <FormItem label="名称：" prop="userNick">
          <Input v-model="formModal.userNick"/>
        </FormItem>
        <FormItem label="角色：" prop="userRole">
          <Select v-model="formModal.roleId">
            <Option v-for="item in roleList" :value="item.pkid" :key="item.pkid">{{ item.roleName }}</Option>
          </Select>
        </FormItem>
        <FormItem  label="密码：" prop="userPwd">
          初始密码为 123456
        </FormItem>
      </Form>
    </div>

    <div class="formButtonDiv">
      <Button class="buttonDiv" @click="$router.go(-1);">返回</Button>
      <Button class="buttonDiv" type="primary" @click="formSubmit">提交</Button>
    </div>
  </div>
</template>

<script>
  export default {
    name: "addUpdatw",
    data(){
      const validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入登录账号'));
        } else {
          this.$app.post('/tadmin/examineUserName', {
            userName:value
          }).then((res)=>{
            if(res.data.code==0){
              if(res.data.data==0){
                callback();
              }else{
                callback(new Error('重复账号'));
              }
            }else{
              this.$Message.error("请求失败");
            }
            // console.log(res);
          }).catch((res)=>{
            //console.log(response);
          });

        }
      };
      return {
        roleList:[
          // {isRoot: 1, pkid: 1, roleCode: "root", roleName: "系统管理员"},
          // {isRoot: 0, pkid: 2, roleCode: "admin1", roleName: "管理员1"},
          // {isRoot: 0, pkid: 3, roleCode: "admin2", roleName: "管理员2"}
        ],
        formModal:{
          userName:'',
          userNick:'',
          roleId:1,
          pkid:0,
          // userPwd:'',
        },
        formMsgRule: {
          userName: [
            {required: true, message: '请输入登录账号', trigger: 'blur'},
            { validator: validatePass, trigger: "blur" },
          ],
          userRole:[
            {required: true, message: '选择角色', trigger: 'blur'},
          ],
          userPwd: [
            {required: true, message: '密码', trigger: 'blur'},
          ],
        },
      }
    },
    methods:{
      formSubmit(){
        let that=this;
        return new Promise(resolve => {
          this.$app.post('/tadmin/updateNewAdmin', that.formModal).then((res)=>{
            if(res.data.code==0){
              // console.log(res.data.data.list);
              that.roleList=res.data.data.list
              resolve();


              that.$router.push({
                name:'adminController',
              })
              that.$Message.success("修改管理员成功");
            }else{
              that.$Message.error("修改管理员失败");
            }
          })
        })
      },
      getAllAdminRole(){
        let that=this;
        return new Promise(resolve => {
          this.$app.post('/trole/getAllRole', {}).then((res)=>{
            if(res.data.code==0){
              // console.log(res.data.data.list);
              that.roleList=res.data.data.list
              resolve();

            }else{
              that.$Message.error("获取管理员角色失败");
            }
          })
        })
      },
      getAdminInfo(pkid){
        let that=this;
        return new Promise(resolve => {
          this.$app.post('/tadmin/getAdminInfoById', {
            pkid:pkid
          }).then((res)=>{
            if(res.data.code==0){
              // console.log("===>",res.data.data);
              that.formModal=res.data.data
              resolve();
            }else{
              that.$Message.error("获取管理员信息失败");
            }
          })
        })
      },
    },
    mounted() {
      // this.formModal.pkid;
      let pkid=this.$route.query.adminId;
      if(pkid>0){
        this.getAdminInfo(pkid);
      }else{
        this.$Message.warning("没有接受到参数")
      }
      this.getAllAdminRole();
    }
  }
</script>

<style lang="less" scoped>
  #insertDiv{
    width: 60%;
    margin: 0 auto;
    padding-top: 10em;

    .formButtonDiv{
      float: right;
      & .buttonDiv{
        /*margin: 0 .5em;*/
      }
    }
  }
</style>
